from fastapi import APIRouter, Depends
from sqlmodel import Session, select
from starlette.requests import Request
from models.user import User
from plugin.database import get_db

user_router = APIRouter(prefix="/user", tags=["用户接口"])


@user_router.get("/", summary="获取用户列表")
def user_list(request: Request, limit: int = 10, skip: int = 0):
    res = {
        "code": 0,
        "message": "success",
        "data": []}
    return res


@user_router.get("/{user_id}", summary="获取用户详情")
def user_detail(request: Request, user_id: int, db: Session = Depends(get_db)):
    try:
        query_code = select(User).where(User.id == user_id)
        results = db.exec(query_code).all()
        res = {
            "code": 0,
            "message": "success",
            "data": results}
    except Exception as e:
        res = {
            "code": 0,
            "message": "success",
            "data": results}
    return res


@user_router.post("/", summary="新增用户")
def create_user(item: User, db: Session = Depends(get_db)):
    try:
        db_user = User.model_validate(item)
        db.add(db_user)
        db.commit()
        res = {
            "code": 200,
            "message": "success"}
    except Exception as e:
        db.rollback()
        res = {
            "code": 0,
            "message": e}
    return res


@user_router.delete("/{user_id}", summary="删除用户")
def delete_user(user_id: int):
    res = {
        "code": 0,
        "message": "success",
        "data": []}
    return res


@user_router.put("/{user_id}", summary="编辑用户")
def update_user(request: Request, user_id: int, data: dict):
    res = {
        "code": 0,
        "message": "success",
        "data": []}
    return res


@user_router.post("/login", summary="用户登录")
def user_login(request: Request, data: dict):
    res = {
        "code": 0,
        "message": "success",
        "data": []}
    return res
